package com.wy.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wy.common.result.Result;
import com.wy.model.entity.Student;
import com.wy.model.vo.StudentQueryVo;
import com.wy.system.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/system/student")
public class StudentController {
  @Autowired
  private StudentService studentService;

  //查询所有Student
  @GetMapping("findAll")
  public Result findAllStudent(){
    List<Student> list = studentService.list();
    return Result.ok(list);
  }

  //删除Student
  @DeleteMapping("remove/{id}")
  public Result removeStudent(@PathVariable("id") Integer id){
    boolean isSuccess = studentService.removeById(id);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  //批量删除
  @DeleteMapping("batchRemove")
  public Result batchRemove(@RequestBody List<Integer> ids){
    boolean isSuccess = studentService.removeByIds(ids);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  //分页查询Student;page表当前页 limit每页记录数
  @GetMapping("{page}/{limit}")
  public Result findQueryStudent(@PathVariable Integer page,
                                 @PathVariable Integer limit,
                                 StudentQueryVo studentQuery){
    //创建page对象
    Page<Student> pageParam = new Page<>(page,limit);
    //调用service方法
    IPage<Student> pageModel = studentService.selectPage(pageParam,studentQuery);
    //返回
    return Result.ok(pageModel);
  }

  //添加Student对象
  //@RequestBody 不能使用get提交方式
  //传递json个是数据，把json格式数据封装到对象里面
  @PostMapping("save")
  public Result saveStudent(@RequestBody Student student){
    boolean isSuccess = studentService.save(student);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  //修改 根据id查询
  @GetMapping("findStudentById/{id}")
  public Result findStudentById(@PathVariable("id") Integer id){
    Student student = studentService.getById(id);
    return Result.ok(student);
  }

  //修改 更新数据库
  @PostMapping("update")
  public Result updateStudent(@RequestBody Student student){
    boolean isSuccess = studentService.updateById(student);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }
}
